package com.ruoyi.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.base.domain.vo.BdTreeVo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.base.domain.bo.BaseBsFbfxBo;
import com.ruoyi.base.domain.vo.BaseBsFbfxVo;
import com.ruoyi.base.domain.BaseBsFbfx;
import com.ruoyi.base.mapper.BaseBsFbfxMapper;
import com.ruoyi.base.service.IBaseBsFbfxService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 分部分项Service业务层处理
 *
 * @author zxs
 * @date 2024-10-16
 */
@RequiredArgsConstructor
@Service
public class BaseBsFbfxServiceImpl implements IBaseBsFbfxService {

    private final BaseBsFbfxMapper baseMapper;

    /**
     * 查询分部分项
     */
    @Override
    public BaseBsFbfxVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询分部分项列表
     */
    @Override
    public TableDataInfo<BaseBsFbfxVo> queryPageList(BaseBsFbfxBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseBsFbfx> lqw = buildQueryWrapper(bo);
        Page<BaseBsFbfxVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询分部分项列表
     */
    @Override
    public List<BaseBsFbfxVo> queryList(BaseBsFbfxBo bo) {
        LambdaQueryWrapper<BaseBsFbfx> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseBsFbfx> buildQueryWrapper(BaseBsFbfxBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseBsFbfx> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getZdId() != null, BaseBsFbfx::getZdId, bo.getZdId());
        lqw.eq(bo.getParentId() != null, BaseBsFbfx::getParentId, bo.getParentId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), BaseBsFbfx::getName, bo.getName());
        lqw.eq(bo.getType() != null, BaseBsFbfx::getType, bo.getType());
        lqw.eq(bo.getSort() != null, BaseBsFbfx::getSort, bo.getSort());
        return lqw;
    }

    /**
     * 新增分部分项
     */
    @Override
    public Boolean insertByBo(BaseBsFbfxBo bo) {
        BaseBsFbfx add = BeanUtil.toBean(bo, BaseBsFbfx.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改分部分项
     */
    @Override
    public Boolean updateByBo(BaseBsFbfxBo bo) {
        BaseBsFbfx update = BeanUtil.toBean(bo, BaseBsFbfx.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseBsFbfx entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除分部分项
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<BdTreeVo> getTreelist(BaseBsFbfxBo bo) {

        return null;
    }
}
